package list;

import java.util.List;

public class MySingleLinkedList implements IList {

    //创建一个内部类，用来表示节点类型
    class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    //创建一个节点类型的头节点变量
    public ListNode head;

    //创建一个构造链表的方法
    public void creatList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);

        //将节点连接起来
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;

        //将头结点指向 node1
        this.head = node1;
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void addFirst(int val) {
        ListNode node = new ListNode(val);
        node.next = head;
        head = node;
    }

    @Override
    public void addLast(int val) {
        ListNode cur = head;
        ListNode node = new ListNode(val);
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;

    }

    @Override
    public void addIndex(int index, int val) {
        ListNode node = new ListNode(val);

        //检查输入的 index 是否合法
        try {
            checkIndex(index);
        } catch (IndexNotLegalException e) {
            e.printStackTrace();
        }

        //当 index == 0 || index == size() 时
        if (index == 0) {
            addFirst(val);
            return;
        } else if (index == size()) {
            addLast(val);
            return;
        }

        ListNode cur = head;
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        node.next = cur.next;
        cur.next = node;
    }

    public void checkIndex(int index) {
        if (index < 0 || index > size()) {
            throw new IndexNotLegalException("index位置不合法");
        }
    }

    @Override
    public boolean contains(int val) {
        ListNode cur = head;
        while (cur != null){
            if(cur.val == val)
                return true;
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(head == null)
            return;
        ListNode cur = head;

        //当第一个元素就是 key
        if(head.val == key){
            head = head.next;
        }

        //要找到目标节点的前一位才能进行删除操作
        while (cur.next != null) {
            if (cur.next.val == key) {
                ListNode del = cur.next;
                cur.next = del.next;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAll(int key) {
        //首先判断是否是空链表
        if(head == null)
            return;

        //定义 prev 和 cur 节点
        ListNode prev = head;
        ListNode cur = prev.next;

        //进行删除
        while(cur != null) {
            if(cur.val == key)
                prev.next = cur.next;
            else
                prev = cur;
            cur = cur.next;
        }

        //处理头节点
        if(head.val == key)
            head = head.next;

    }

}
